//
//  AppDelegate.m
//  Cup
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 doll. All rights reserved.
//

#import "AppDelegate.h"
#import "TodayListViewController.h"
#import "RadioCollectionViewController.h"
#import "ReadViewController.h"
#import "CommunityViewController.h"
#import "UMSocial.h"
#import "dataBaseHandle.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.window.bounds];
    imageView.image = [UIImage imageNamed:@"welcome"];
    [self.window addSubview:imageView];
    [imageView release];
    
    imageView.alpha = 1.0;
    [UIView animateWithDuration:2.5f delay:1.0f options:UIViewAnimationOptionCurveLinear
                     animations:^
     {
         imageView.alpha = 0.6;
     }
                     completion:^(BOOL finished)
     {
         [imageView removeFromSuperview];
         [NSThread sleepForTimeInterval:0.3f];
         
         TodayListViewController *todayVC = [[TodayListViewController alloc] init];
         todayVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"today"] tag:1000];
         UINavigationController *todayNavigation = [[UINavigationController alloc] initWithRootViewController:todayVC];
         [todayVC release];
         todayNavigation.navigationBar.barTintColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
         todayNavigation.navigationBar.translucent = NO;
         
         RadioCollectionViewController *radioVC = [[RadioCollectionViewController alloc] init];
         radioVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"电台" image:[UIImage imageNamed:@"radio"] tag:1001];
         UINavigationController *radioNavigation = [[UINavigationController alloc] initWithRootViewController:radioVC];
         [radioVC release];
         radioNavigation.navigationBar.barTintColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
         radioNavigation.navigationBar.translucent = NO;
         
         ReadViewController *readVC = [[ReadViewController alloc] init];
         readVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"阅读" image:[UIImage imageNamed:@"read"] tag:1002];
         UINavigationController *readNavigation = [[UINavigationController alloc] initWithRootViewController:readVC];
         [readVC release];
         readNavigation.navigationBar.barTintColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
         readNavigation.navigationBar.translucent = NO;
         
         CommunityViewController *communityVC = [[CommunityViewController alloc] init];
         communityVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"社区" image:[UIImage imageNamed:@"community"] tag:1003];
         UINavigationController *communityNavigation = [[UINavigationController alloc] initWithRootViewController:communityVC];
         [communityVC release];
         communityNavigation.navigationBar.barTintColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
         communityNavigation.navigationBar.translucent = NO;
         
         UITabBarController *myTabBar = [[UITabBarController alloc] init];
         myTabBar.viewControllers = [NSArray arrayWithObjects:todayNavigation,radioNavigation,readNavigation,communityNavigation, nil];
         self.window.rootViewController = myTabBar;
         myTabBar.tabBar.translucent = NO;
         myTabBar.tabBar.barTintColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
         myTabBar.tabBar.tintColor = [UIColor colorWithRed:59/255.0 green:54/255.0 blue:24/255.0 alpha:1];
         
         [todayNavigation release];
         [radioNavigation release];
         [readNavigation release];
         [communityNavigation release];
     }
     ];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [dataBaseHandle shareDB].delegate = self;
    [dataBaseHandle shareDB].islogin =NO;
    [UMSocialData setAppKey:@"553afad967e58e7cba0031e4"];
    
    
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                NSLog(@"未知网络");
                [dataBaseHandle shareDB].state = 1;
                break;
                
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                NSLog(@"没有网络(断网)");
                [dataBaseHandle shareDB].state = 0;
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                NSLog(@"手机自带网络");
                [dataBaseHandle shareDB].state = 1;
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                NSLog(@"WIFI");
                [dataBaseHandle shareDB].state = 1;
                break;
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];

    [self.window release];
    return YES;
}

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (void)changeNight
{
    self.window.alpha = 0.5;
}

- (void)changeDay
{
    self.window.alpha = 1;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
